#include "OGREViewer.h"
#include <OgreSubEntity.h>


OGREViewer::OGREViewer()
: mpEntity( NULL ),
  mpEntityNode( NULL ),
  mpAnimationStateSet( NULL ),
  mpAnimationState( NULL ),
  mEntityDB1(),
  mEntityDB2(),
  mpEntityCheck( NULL ),
  mpEntityMenu( NULL ),
  mpAnimationMenu( NULL ),
  mpEntityInfoPanel( NULL ),
  mpSpeedSlider( NULL ),
  mSpeed( 1.0f )
{
}

OGREViewer::~OGREViewer()
{
}

bool OGREViewer::InitializeSceneEntity()
{
	mpEntityNode = mpSceneManager->getRootSceneNode()->createChildSceneNode();
	assert( mpEntityNode );

	Ogre::ResourceGroupManager& rRCManager = Ogre::ResourceGroupManager::getSingleton();

	Ogre::StringVector RCGroups = rRCManager.getResourceGroups();

	for( int i = 0; i < RCGroups.size(); ++ i )
	{
		Ogre::StringVectorPtr resource = rRCManager.listResourceNames( RCGroups.at( i ) );

		for( int j = 0; j < resource->size(); ++j )
		{
			Ogre::String& s = resource->at( j );

			if( s.substr( s.find_last_of( "." ) + 1 ) == "mesh" )
			{
				Ogre::String name = Ogre::String( s, 0, s.size() - 5 );
				Ogre::Entity* pEntity = mpSceneManager->createEntity( name, s );
				assert( pEntity );

				Ogre::AnimationStateSet* pSet = pEntity->getAllAnimationStates();

				if( pSet ) mEntityDB1.push_back( name );
				else mEntityDB2.push_back( name );
			}
		}
	}

	return true;
}

bool OGREViewer::InitializeUI()
{
	OGREApp::InitializeUI();

	mpCameraMan->setStyle( OgreBites::CS_ORBIT );

	mpEntityCheck = mpUI->createCheckBox( OgreBites::TL_TOPLEFT, "MyCheckbox", "Entity No Animation", 340 );

	mpEntityMenu = mpUI->createLongSelectMenu( OgreBites::TL_TOPLEFT, "MyMenu1", "Entity", 340, 200, 20, mEntityDB2 );
	assert( mpEntityMenu );

	mpAnimationMenu = mpUI->createLongSelectMenu( OgreBites::TL_TOPLEFT, "MyMenu2", "Animation", 340, 200, 10, Ogre::StringVector() );
	assert( mpAnimationMenu );

	mpEntityInfoPanel = mpUI->createParamsPanel( OgreBites::TL_BOTTOMLEFT, "MyPanel", 340, 10 );

	itemSelected( mpEntityMenu );

	mpSpeedSlider = mpUI->createLongSlider( OgreBites::TL_TOPLEFT, "MySlider", "Speed", 160, 100, 0, 100, 5 );
	mpSpeedSlider->setValue( 50 );

	return true;
}

bool OGREViewer::frameRenderingQueued( const Ogre::FrameEvent& evt )
{
	if( mpAnimationState ) mpAnimationState->addTime( evt.timeSinceLastFrame * mSpeed );

	return OGREApp::frameRenderingQueued( evt );
}

void OGREViewer::buttonHit( OgreBites::Button* button )
{
	//if( button->getTrayLocation() == OgreBites::TL_TOP )
	//{
	//	mpUI->destroyAllWidgetsInTray( OgreBites::TL_LEFT );

	//	if( button->getName().compare( "Animation" ) == 0 )
	//	{
	//		//for( int i = 0; i < mEntityDB1.size(); ++i )
	//		{
	//			//mpUI->createButton( OgreBites::TL_LEFT, mEntityDB1.at( i ), mEntityDB1.at( i ) );
	//			
	//		}
	//	}
	//	else
	//	{
	//		for( int i = 0; i < mEntityDB2.size(); ++i )
	//		{
	//			mpUI->createButton( OgreBites::TL_LEFT, mEntityDB2.at( i ), mEntityDB2.at( i ) );
	//			mpUI->getTrayContainer( OgreBites::TL_RIGHT )->hide();
	//		}
	//	}
	//}
	//else if( button->getTrayLocation() == OgreBites::TL_LEFT )
	//{
	//	if( mpEntity )
	//	{
	//		Ogre::MeshManager* pManager = Ogre::MeshManager::getSingletonPtr();
	//		mpEntityNode->detachObject( mpEntity );
	//		mpEntity = NULL;

	//		//mpUI->clearTray( OgreBites::TL_RIGHT );
	//		mpUI->destroyAllWidgetsInTray( OgreBites::TL_RIGHT );
	//	}

	//	mpEntity = mpSceneManager->getEntity( button->getName() );
	//	assert( mpEntity );

	//	mpEntityNode->attachObject( mpEntity );

	//	mpAnimationStateSet = mpEntity->getAllAnimationStates();
	//	if( mpAnimationStateSet )
	//	{
	//		Ogre::AnimationStateIterator iterator = mpAnimationStateSet->getAnimationStateIterator();

	//		while( iterator.hasMoreElements() )
	//		{
	//			Ogre::AnimationState* pAnimationState = iterator.getNext();

	//			mpUI->createButton( OgreBites::TL_RIGHT, pAnimationState->getAnimationName(), pAnimationState->getAnimationName() );
	//		}

	//		mpUI->getTrayContainer( OgreBites::TL_RIGHT )->show();
	//	}
	//	else
	//	{
	//		mpUI->getTrayContainer( OgreBites::TL_RIGHT )->hide();
	//	}
	//}
	//else if( button->getTrayLocation() == OgreBites::TL_RIGHT )
	//{
	//	if( mpAnimationState )
	//	{
	//		mpAnimationState->setEnabled( false );
	//		mpAnimationState = NULL;
	//	}

	//	mpAnimationState = mpEntity->getAnimationState( button->getName() );
	//	assert( mpAnimationState );

	//	mpAnimationState->setLoop( true );
	//	mpAnimationState->setEnabled( true );
	//}
}

void OGREViewer::itemSelected( OgreBites::SelectMenu* menu )
{
	if( menu == mpEntityMenu )
	{
		if( mpEntity )
		{
			mpEntityNode->detachObject( mpEntity );
			mpEntity = NULL;
		}

		mpEntity = mpSceneManager->getEntity( menu->getSelectedItem() );
		assert( mpEntity );

		mpEntityNode->attachObject( mpEntity );

		mpCameraMan->setTarget( mpEntityNode );

		if( mpAnimationStateSet )
		{
			mpAnimationMenu->clearItems();

			mpAnimationStateSet = NULL;
		}

		mpAnimationStateSet = mpEntity->getAllAnimationStates();
		if( mpAnimationStateSet )
		{
			Ogre::AnimationStateIterator iterator = mpAnimationStateSet->getAnimationStateIterator();
			Ogre::StringVector animations;

			while( iterator.hasMoreElements() )
			{
				Ogre::AnimationState* pAnimationState = iterator.getNext();
				animations.push_back( pAnimationState->getAnimationName() );
			}

			mpAnimationMenu->setItems( animations );

			itemSelected( mpAnimationMenu );
		}

		unsigned int count = mpEntity->getNumSubEntities();

		Ogre::StringVector items;
		items.push_back( "SubEntity Count" );
		items.push_back( "Sub1 Material" );

		//for( unsigned int i = 0; i < count; ++i )
		//{
		//	Ogre::String submesh( "SubMesh" );
		//	submesh += Ogre::StringConverter::toString( i );

		//	items.push_back( submesh + " Name" );
		//}

		mpEntityInfoPanel->setAllParamNames( items );

		mpEntityInfoPanel->setParamValue( 0 , Ogre::StringConverter::toString( count ) );
		mpEntityInfoPanel->setParamValue( 1 , mpEntity->getSubEntity(0)->getMaterialName() );

		//for( unsigned int i = 1; i < count; ++i )
		//{
		//	mpEntity->getSubEntity()->
		//	mpEntityInfoPanel->setParamValue( i ,  );
		//}

	}
	else if( menu == mpAnimationMenu )
	{
		if( mpAnimationState )
		{
			mpAnimationState->setEnabled( false );
		}

		mpAnimationState = mpEntity->getAnimationState( menu->getSelectedItem() );
		mpAnimationState->setEnabled( true );
		//mpEntity->setDisplaySkeleton( true );
	}
}

void OGREViewer::checkBoxToggled( OgreBites::CheckBox* box )
{
	if( box == mpEntityCheck )
	{
		if( box->isChecked() )
		{
			mpEntityCheck->setCaption( "Entity With Animation" );
			mpEntityMenu->clearItems();
			mpEntityMenu->setItems( mEntityDB1 );
		}
		else
		{
			mpEntityCheck->setCaption( "Entity No Animation" );
			mpEntityMenu->clearItems();
			mpEntityMenu->setItems( mEntityDB2 );
		}

		itemSelected( mpEntityMenu );
	}
}

void OGREViewer::sliderMoved( OgreBites::Slider* slider )
{
	// 0 - 100 -> 0.2 - 1.2
	mSpeed = ( slider->getValue() / 100 ) + 0.2 ;
}
